package com.dhl.springbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author danghailiang
 * 高级篇消息确认机制
 */
@Configuration
public class ConfirmConfig {
    /**
     * 交换机name
     */
    public static final String CONFIRM_EXCHANGE_NAME="confirm.exchange";
    /**
     * 队列name
     */
    public static final String CONFIRM_QUEUE_NAME="confirm.queue";
    /**
     * 路由key
     */
    public static final String CONFIRM_ROUTING_KEY="KEY1";


    /**
     * 备份交换机name
     */
    public static final String BACKUP_EXCHANGE_NAME="backup.exchange";

    /**
     * 备份队列name
     */
    public static final String BACKUP_QUEUE_NAME="backup.queue";
    /**
     * 预警队列name
     */
    public static final String WARNING_QUEUE_NAME="warning.queue";

    @Bean
    public DirectExchange confirmExchange(){
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME)
                .durable(true)
                // 设置为备用交换机
                .alternate(BACKUP_EXCHANGE_NAME)
                .build();
    }
    @Bean
    public Queue confirmQueue(){
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
    }
    @Bean
    public Binding queueBindingExchange(Queue confirmQueue,  DirectExchange confirmExchange){
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);
    }
    // ==========================声明备份交换机和队列====================================
    @Bean
    public FanoutExchange backupExchange(){
        return ExchangeBuilder.fanoutExchange(BACKUP_EXCHANGE_NAME).build();
    }

    @Bean
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }

    @Bean
    public Queue warningQueue(){
        return QueueBuilder.durable(WARNING_QUEUE_NAME).build();
    }

    @Bean
    public Binding backupQueueBindingExchange(Queue backupQueue,  FanoutExchange backupExchange){
        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }
    @Bean
    public Binding warningQueueBindingExchange(Queue warningQueue,  FanoutExchange backupExchange){
        return BindingBuilder.bind(warningQueue).to(backupExchange);
    }
}
